import * as LoginApi from "../../api/login";
const util = require("../../utils/util");
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    bg: "https://lend-1328081244.cos.ap-nanjing.myqcloud.com/miniprogram_image%2Findex_2.png",
    windowHeight: 0,
    scrollViewHeight: 300,
    isBind: false,
    token: "",
    pathUrl: "",
    scene: "",
    btnType: "scanCode",
    visibleDialog: false,
    isAgreement: false,
    indexVisibleDialog: false,
    show: true,
    wxScene: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log("options", options);
    console.log("options", Object.values(options).length);
    const wxScene = app.globalData.wxScene;
    const is_login =
      wxScene === 1047 || wxScene === 1048 || wxScene === 1049
        ? app.globalData.is_login
        : wx.getStorageSync("is_login");
    console.log("is_login11111", is_login);

    // if (options.wxScene) {
    //   this.setData({
    //     wxScene: options.wxScene,
    //     scene: options.scene,
    //     visibleDialog: !app.globalData.is_login,
    //   });
    //   console.log("is_login2222", app.globalData.is_login);
    //   console.log("visibleDialog", this.data.visibleDialog);
    // }
    this.setData({
      scene: options.scene || "",
      visibleDialog: options.scene && !is_login,
    });

    const _this = this;
    wx.getSystemInfo({
      success: (res) => {
        const windowHeight = res.windowHeight;
        _this.setData({ windowHeight });
      },
    });
  },
  onShow() {},
  handleBackEvent() {
    this.setData({ show: true });
  },

  /**
   * @description: 获取用户信息
   */

  /**
   * @description: 个人中心
   */
  goToMyCenter() {
    this.setData({ btnType: "myCenter" });
    if (wx.getStorageSync("is_login")) {
      wx.navigateTo({ url: "/pages/my/my" });
    } else {
      this.setData({ visibleDialog: true });
    }
  },
  handleRadio(e) {
    this.setData({ isAgreement: e.detail });
  },
  handlePolicy() {
    wx.navigateTo({
      url: `/pages/webview/webview?link=${app.globalData.agreement}`,
    });
  },
  handleToast() {
    wx.showToast({ title: "请勾选协议", icon: "none" });
  },
  /**
   * @description: 手机号一键登录
   * @param {*} detail
   * @return {*}
   */
  getPhoneNumber({ detail }) {
    const _this = this;
    wx.login({
      success(_res) {
        const _body = {
          encrypted_data: detail.encryptedData,
          iv: detail.iv,
          code: _res.code,
        };
        console.log(
          'detail.hasOwnProperty("encryptedData")',
          detail.hasOwnProperty("encryptedData")
        );
        console.log("detail.encryptedData", detail.encryptedData);

        if (detail.hasOwnProperty("encryptedData") && detail.encryptedData) {
          LoginApi.bindMobile(app, _body).then((res) => {
            console.log(res);

            app.globalData.token = res.token;
            wx.setStorageSync("token", res.token);
            wx.setStorageSync("is_login", true);
            app.globalData.is_login = true;
            _this.setData({
              visibleDialog: !_this.data.visibleDialog,
            });
            if (_this.data.btnType == "myCenter") {
              wx.navigateTo({ url: "/pages/my/my" });
            }
            console.log(_this.data.btnType);

            if (_this.data.btnType == "scanCode" && _this.data.scene) {
              console.log("_this.data.btnType", _this.data.btnType);
              console.log("_this.data.scene", _this.data.scene);

              wx.navigateTo({
                url: `/pages/cabinetInfo/cabinetInfo?scene=${_this.data.scene}`,
              });
            }

            if (_this.data.btnType == "scanCode" && !_this.data.scene) {
              _this.handleScanCode();
            }
          });
        }
      },
    });
  },
  /**
   * @description: 其他手机号登录
   * @return {*}
  //  */
  handleOtherLogin() {
    if (!this.data.isAgreement)
      return wx.showToast({ title: "请勾选协议", icon: "none" });

    wx.navigateTo({ url: "/pages/login/login" });
  },
  /**
   * @description: 扫码
   * @return {*}
   */
  handleScanCode() {
    this.setData({ btnType: "scanCode" });
    const _this = this;
    if (wx.getStorageSync("is_login")) {
      /**
       * @description: 扫码成功回调
       * @param {object} res 扫码结果
       * @property {string} res.path 扫码结果
       * @return {*}
       */
      wx.scanCode({
        success(res) {
          console.log(res);
          _this.setData({ show: false });
					const bValue = util.getQueryString(res.result, "scene");
          // const bValue = "10000002";
          console.log(bValue);
          // const cabinet_code = 88;
          wx.navigateTo({
            url: `/pages/cabinetInfo/cabinetInfo?scene=${bValue}`,
          });
        },
      });
    } else {
      this.setData({ visibleDialog: true });
    }
  },
  goToPages(e) {
    let url = e.currentTarget.dataset.url;
    if (url) {
      wx.navigateTo({ url });
    }
  },
  handleCloseDialog() {
    this.setData({ visibleDialog: false });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    // this.setData({show: true})
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
